package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseTableListAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.MaterieListOriginal;
import com.isunland.managesystem.entity.MaterieListOriginal.MaterieListContent;
import com.isunland.managesystem.entity.OderSaleContent;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

@SuppressWarnings("ConstantConditions") public class AddMaterielListFragment
    extends BaseListFragment {
  public static final String EXTRA_CONTENT = "com.isunland.managesystem.entity.EXTRA_CONTENT";
  private static final int ADD_MATERIEL = 0;
  private static final int DETAIL_MATERIEL = 1;
  private ArrayList<MaterieListContent> mList;
  //	private MaterieLisAdapter mAdapter;

  private BaseTableListAdapter mAdapter;
  private static String orderId = "0";
  private static OderSaleContent orderContent = new OderSaleContent();

  //    private BroadcastReceiver mOnShowNotification = new BroadcastReceiver() {
  //
  //        @Override
  //        public void onReceive(Context context, Intent intent) {
  //        	LogUtil.i("接收广播=====");
  //        	orderContent=(OderSaleContent) intent.getSerializableExtra(EXTRA_CONTENT);
  //        	LogUtil.i("mContent====="+orderContent.getId().toString());
  //        	orderId=orderContent.getId();
  //
  //        	LogUtil.i("数据状态mContent====="+orderContent.getMoutbillStatus());
  //        }
  //    };
  //
  //    @Override
  //    public void onResume() {
  //        super.onResume();
  //        IntentFilter filter = new IntentFilter(AddOrderSaleFragment.ACTION_SHOW_NOTIFICATION);
  //        getActivity().registerReceiver(mOnShowNotification, filter, AddOrderSaleFragment.PERM_PRIVATE, null);
  //
  //    };
  //
  //    @Override
  //    public void onPause() {
  //        super.onPause();
  //        getActivity().unregisterReceiver(mOnShowNotification);
  //    }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    //接收ordercontent
    orderContent = (OderSaleContent) getArguments().getSerializable(EXTRA_CONTENT);
    orderId = orderContent.getId();
    LogUtil.i("mContent=====" + orderId);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

        break;
      case R.id.menu_item_add:
        if ("0".equals(orderId)) {
          ToastUtil.show(R.string.save_main);
        } else {
          Intent i = new Intent(getActivity(), AddMaterielDetailActivity.class);
          i.putExtra(AddMaterielDetailFragment.EXTRA_CONTENT, orderContent);
          startActivityForResult(i, ADD_MATERIEL);
          //				LogUtil.i("mainId===="+mContent.getId().toString());
        }

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {

    return ApiConst.URL_MATERIE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainId", orderId);
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    MaterieListOriginal original = new Gson().fromJson(arg0, MaterieListOriginal.class);
    ArrayList<MaterieListContent> rows = (ArrayList<MaterieListContent>) original.getRows();
    if (mList == null) {
      mList = new ArrayList<>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<MaterieListContent> mList2) {
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put("物料名称", "materialName");
    map.put("出库数量", "moutNumber");
    map.put("单价", "moutPrice");
    if (mAdapter == null) {
      mAdapter = new BaseTableListAdapter<>(getActivity(), mList2, map,
          new MaterieListOriginal().new MaterieListContent());
    }
    setListAdapter(mAdapter);
    ((BaseTableListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  //点击跳转到物料详情界面
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MaterieListContent mContent = mList.get(position - 1);
    Intent i = new Intent(getActivity(), ShowMaterieDetailActivity.class);
    i.putExtra(ShowMaterieDetailFragment.EXTRA_CONTENT, mContent);
    i.putExtra(ShowMaterieDetailFragment.EXTRA_ORDER_CONTENT, orderContent);
    startActivityForResult(i, DETAIL_MATERIEL);
    super.onListItemClick(l, v, position, id);
  }

  public void refresh() {
    volleyPost();
  }

  public static void getContent(OderSaleContent item) {
    orderContent = item;
    orderId = item.getId();
    LogUtil.i("更新后mContent  id=====" + orderId);
    LogUtil.i("更新后mContent  storageid=====" + orderContent.getStorageId());
  }

  public static AddMaterielListFragment newInstance(OderSaleContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    AddMaterielListFragment fragment = new AddMaterielListFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == ADD_MATERIEL) {
      volleyPost();
    }
    if (requestCode == DETAIL_MATERIEL) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}

